//
//  MadePlanViewController.m
//  liangyi
//
//  Created by 张水 on 16/12/19.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "MadePlanViewController.h"
#import "AppHeader.h"
#import "ColorButton.h"
#import "PlanModel.h"
@interface MadePlanViewController ()//<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong) UIPickerView *diseasePickView;

@property(nonatomic,strong) UITextField *diseaseField;

@property(nonatomic,strong) UIButton *timeBtn;

@property(nonatomic,strong) UIButton  *submitBtn;

@property(nonatomic,strong) NSMutableArray *dataArr;

@property(nonatomic,strong) GFCalendarView *calendar;

@property(nonatomic,copy)   NSString *schemeId;

@property(nonatomic,copy)   NSString *operationTime;

@property(nonatomic,strong) UIView  *backView;

@end

@implementation MadePlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [NSMutableArray array];
    self.title = @"定制方案";
    [self LoadDisease];
    [self setUpUi];
}
-(void)setUpUi{
    self.view.backgroundColor = [UIColor ColorWithHex:@"#ecf1f2"];
    UIView *backView1 = [[UIView alloc]init];
    backView1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView1];
    [backView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        make.height.equalTo(@60);
    }];
    UILabel *oneDesLabel = [[UILabel alloc]init];
    oneDesLabel.textColor = [UIColor ColorWithHex:@"#686868"];
    oneDesLabel.font = [UIFont systemFontOfSize:17];
    oneDesLabel.text = @"1.  您的病症名称?";
    [backView1 addSubview:oneDesLabel];
    [oneDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView1).offset(20);
        make.centerY.equalTo(backView1.mas_centerY);
    }];
    
    UIView *backView2 = [[UIView alloc]init];
    backView2.backgroundColor = [UIColor ColorWithHex:@"#ecf1f2"];
    [self.view addSubview:backView2];
    [backView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(backView1.mas_bottom);
        make.height.equalTo(@90);
    }];
    
    
    
    self.diseaseField = [[UITextField alloc]init];
    self.diseaseField.placeholder = @"请选择病症";
    [self.diseaseField  setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.diseaseField  setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [self.view addSubview:self.diseaseField];
    [self.diseaseField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX).offset(48);
        make.centerY.equalTo(backView2.mas_centerY);
        make.width.equalTo(@170);
        make.height.equalTo(@40);
    }];
//    self.diseasePickView = [[UIPickerView alloc]init];
//    self.diseasePickView.delegate = self;
//    self.diseasePickView.dataSource = self;
//    self.diseaseField.inputView = self.diseasePickView;
//    [self.diseaseField addTarget:self action:@selector(LoadDisease) forControlEvents:UIControlEventEditingDidBegin];
    
    
    UIView *backView3 = [[UIView alloc]init];
    backView3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView3];
    [backView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(backView2.mas_bottom);
        make.height.equalTo(@60);
    }];
    
    UILabel *twoDesLabel = [[UILabel alloc]init];
    twoDesLabel.textColor = [UIColor ColorWithHex:@"#686868"];
    twoDesLabel.font = [UIFont systemFontOfSize:17];
    twoDesLabel.text = @"2.  您的手术时间?";
    [backView3 addSubview:twoDesLabel];
    [twoDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView3).offset(20);
        make.centerY.equalTo(backView3.mas_centerY);
    }];
    
    self.timeBtn = [[UIButton alloc]init];
    [self.view addSubview:self.timeBtn];
    [self.timeBtn setTitle:@"_____ 年 _____ 月 _____ 日" forState:UIControlStateNormal];
    [self.timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView3.mas_bottom).offset(30);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [self.timeBtn addTarget:self action:@selector(selectTime) forControlEvents:UIControlEventTouchUpInside];
    [self.timeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
     NSMutableArray *colorArray = [@[[UIColor ColorWithHex:@"#5dcedb"],[UIColor ColorWithHex:@"#5ac3d1"]] mutableCopy];
    self.submitBtn = [[ColorButton alloc]initWithFrame:CGRectMake(80, 360, 300, 50) FromColorArray:colorArray ByGradientType:topToBottom];
    self.submitBtn.layer.cornerRadius = 10;
    self.submitBtn.layer.masksToBounds = YES;
    [self.submitBtn setTitle:@"完成定制" forState:UIControlStateNormal];
    [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.submitBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:self.submitBtn];
    [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.timeBtn.mas_bottom).offset(90);
    }];
    [self.submitBtn addTarget:self action:@selector(subimitBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.backView.backgroundColor = [UIColor blackColor];
    self.backView.alpha = 0.4;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeSubViews)];
    [self.backView addGestureRecognizer:tapGesture];
    
}

//-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
//    return 1;
//}
//-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//    return self.dataArr.count;
//}
//-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
////    PlanModel *model = self.dataArr[row];
////    return model.name;
//}
//-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
////    PlanModel *model = self.dataArr[row];
////    self.diseaseField.text = model.name;
////    self.schemeId = model.id;
//}

-(void)LoadDisease{
    
    NSString *url = [BASEURL stringByAppendingString:kURL_SchemeList];
    NSDictionary *dict = @{@"cateId":self.cateId};
    NSLog(@"%@",dict);
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString};
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            if ([network.dataObject isKindOfClass:[NSArray class]]){
                [self.dataArr removeAllObjects];
                NSArray *arr = [PlanModel mj_objectArrayWithKeyValuesArray:network.dataObject];
                NSLog(@"xxx%lu",(unsigned long)arr.count);
                [self.dataArr addObjectsFromArray:arr];
                NSLog(@"%ld",(unsigned long)self.dataArr.count);
            }
            
        }
    } failure:^(id error) {
        
    }];
    
}
-(void)subimitBtnDidClick{
    
    NSString *userLogin = [CommonMethod fuckNULL:[UserDefaultsUtlis getValueForKey:@"userid"]];
    NSLog(@"----%@",userLogin);
    if (userLogin.length == 0) {
        [CustomProgressHUD showSuccessWithStatus:@"请先登录"];
        return;
    }

    
    
    NSString *url = [BASEURL stringByAppendingString:kURL_Plan];
    NSDictionary *dict = @{@"schemeId":self.schemeId,
                           @"operationTime":self.operationTime};
    NSLog(@"%@",dict);
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString,
                                 @"login":[NSString AES128Encrypt:[NSString stringWithFormat:@"%@@@%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"],ApplicationDelegate.deviceID]]};
    
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            [CustomProgressHUD showSuccessWithStatus:@"方案定制成功,请到个人中心查看我的方案"];
        }else {
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error];
    }];
}

-(void)removeSubViews{
    [self.calendar removeFromSuperview];
    [self.backView removeFromSuperview];
}

-(void)selectTime{
    
    [self.view addSubview:self.backView];
    
    CGFloat width = self.view.bounds.size.width - 20;
    CGPoint origin = CGPointMake(10.0, 64.0 + 70);
    
    self.calendar = [[GFCalendarView alloc] initWithFrameOrigin:origin width:width];
    _calendar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_calendar];
    __weak MadePlanViewController *weakSelf = self;
    // 点击某一天的回调
    _calendar.didSelectDayHandler = ^(NSInteger year, NSInteger month, NSInteger day) {

        [weakSelf.timeBtn setTitle:[NSString stringWithFormat:@"%ld年%ld月%ld日", (long)year, (long)month, (long)day] forState:UIControlStateNormal];
        
        weakSelf.operationTime = [NSString stringWithFormat:@"%ld-%ld-%ld",(long)year,(long)month,(long)day];
        
        
        [weakSelf.calendar removeFromSuperview];
        [weakSelf.backView removeFromSuperview];
    };
    
    
}
@end
